package tools.packet

import client.MapleCharacter
import handling.opcode.SendPacketOpcode
import tools.Pair
import tools.data.output.MaplePacketLittleEndianWriter


/**
 * 免责声明：本模拟器源代码下载自ragezone.com，仅用于技术研究学习，无任何商业行为。
 */
object PVPPacket {

    fun updateCount(n2: Int, bl2: Boolean): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_UPDATECOUNT.value)
        mplew.writeInt(n2)
        mplew.write(if (bl2) 1 else 0)

        return mplew.packet
    }

    fun screenEffect(mode: Int): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_SCREEN_EFFECT.value)
        mplew.write(mode)

        return mplew.packet
    }

    fun stateChange(enable: Boolean): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_STATECHANGE.value)
        mplew.write(if (enable) 1 else 2)

        return mplew.packet
    }

    fun teamScore(n2: Int, n3: Int): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_TEAMSCORE.value)
        mplew.writeInt(n2)
        mplew.writeInt(n3)

        return mplew.packet
    }

    fun fieldEnter(n2: Int, list: List<Pair<Int, MapleCharacter>>, n3: Int, bl2: Boolean, n4: Int): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_FIELDENTER.value)
        mplew.write(n2)
        mplew.write(n4)
        mplew.write(if (bl2) 1 else 0)
        if (n2 > 0) {
            mplew.write(n3)
            mplew.writeInt(list.size)
            for (pair in list) {
                mplew.writeInt(pair.left)
                mplew.writeMapleAsciiString(pair.right.name)
                mplew.write(pair.right.stat.hpPercent)
                mplew.write(pair.right.stat.pvpRank)
            }
        }

        return mplew.packet
    }


    fun updateTeamInfo(list: List<Pair<Int, MapleCharacter>>): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()
        mplew.writeShort(SendPacketOpcode.PVP_UPDATETEAMINFO.value)
        mplew.writeInt(list.size)
        for (pair in list) {
            mplew.writeInt(pair.left)
            mplew.writeMapleAsciiString(pair.right.name)
            mplew.write(pair.right.stat.hpPercent)
            mplew.write(pair.right.stat.pvpRank)
        }

        return mplew.packet
    }

    fun iceGage(n2: Int): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_ICEGAGE.value)
        mplew.writeInt(n2)

        return mplew.packet
    }

    fun reviveMessage(message: String): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_REVIVE_MESSAGE.value)
        mplew.writeMapleAsciiString(message)

        return mplew.packet
    }

    fun updateRankInfo(list: List<Pair<Int, MapleCharacter>>, n2: Int): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_UPDATERANKINFO.value)
        mplew.writeShort(list.size)
        for (pair in list) {
            mplew.writeInt(pair.right.id)
            mplew.writeMapleAsciiString(pair.right.name)
            mplew.writeInt(pair.left)
            mplew.write(if (n2 == 0) 0 else pair.right.team + 1)
        }

        return mplew.packet
    }

    fun modeResult(list: List<Pair<Int, MapleCharacter>>, n2: Int, n3: Int, n4: Int): ByteArray {
        val mplew = MaplePacketLittleEndianWriter()

        mplew.writeShort(SendPacketOpcode.PVP_MODERESULT.value)
        mplew.writeInt(list.size)
        for (pair in list) {
            mplew.writeInt(pair.right.id)
            mplew.writeMapleAsciiString(pair.right.name)
            mplew.writeInt(pair.left as Int)
            mplew.writeShort(pair.right.team + 1)
            mplew.writeInt(0)
            mplew.writeInt(0)
        }
        mplew.writeInt(0)
        mplew.writeInt(0)
        mplew.writeInt(0)
        mplew.writeInt(n2)
        mplew.write(0)
        mplew.writeShort(0)
        mplew.writeInt(0)
        mplew.writeInt(0)
        mplew.write(n3)
        mplew.write(n4)

        return mplew.packet
    }
}